package org.skipper.samples.mybatis.plus.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import javax.sql.DataSource;
import java.io.*;
import java.util.Collections;
import java.util.Properties;

public class Generator {

    public static void main(String[] args) {

        Properties props = new Properties();
        BufferedReader br = null;
        String filePath = Generator.class.getClassLoader().getResource("application-connection.properties").getFile();
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
            props.load(br);
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String dbUrl = "jdbc:mysql://rm-uf649baxsg6hxh384mo.mysql.rds.aliyuncs.com:3306/zhongtuo?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
        String dbUser = "zhongtuo";
        String dbPassword = "Zhongtuo2024";

        String author = props.getProperty("author");
        String globalConfigOutputDir = props.getProperty("global.config.output.dir");
        String packageConfigParent = props.getProperty("package.config.parent");
        String packageConfigModuleName = props.getProperty("package.config.module.name");
        String mapperXml = props.getProperty("mapper.xml");
        String tableNames = props.getProperty("table.names");
        String[] tables = tableNames.split(",");

        DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder(dbUrl, dbUser, dbPassword)
                .schema("zhongtuo");

        FastAutoGenerator.create(dataSourceConfigBuilder)
                .templateConfig(builder -> {
                    builder.controller("")//不生成对应controller
                            .service("")//不生成对应service
                            .serviceImpl("");
                })
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .disableOpenDir() //不打开输出目录
                            .outputDir(globalConfigOutputDir); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(packageConfigParent) // 设置父包名
                            .moduleName(packageConfigModuleName) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, mapperXml)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    for (String table : tables) {
                        builder.addInclude(table); // 设置需要生成的表名
                    }
                    builder.entityBuilder().enableLombok(); //使用lombok  会去除entity中get/set改用@Data
                    builder.addTablePrefix("t_", "c_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

}
